package com.gy.assistant.ui.meeting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;

import im.zego.zim.callback.ZIMCallInvitationSentCallback;
import im.zego.zim.callback.ZIMCallingInvitationSentCallback;
import im.zego.zim.entity.ZIMCallInvitationSentInfo;
import im.zego.zim.entity.ZIMCallInviteConfig;
import im.zego.zim.entity.ZIMCallingInvitationSentInfo;
import im.zego.zim.entity.ZIMCallingInviteConfig;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zim.enums.ZIMCallInvitationMode;
import com.gy.assistant.MyApplication;
import com.gy.assistant.R;
import com.gy.assistant.ui.BaseActivity;

public class MeetingInviteActivity extends BaseActivity {
    String callID;
    public static void actionStart(Activity activity,String callId) {
        Intent intent = new Intent(activity, MeetingInviteActivity.class);
        intent.putExtra("callId",callId);
        activity.startActivity(intent);
    }

    private EditText et_member_users;
    private EditText et_timeout;
    private RadioGroup rg_mode;
    private ZIMCallInvitationMode mode = ZIMCallInvitationMode.ADVANCED;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_meeting);
        et_member_users = findViewById(R.id.et_meeting_member_users);
        et_timeout = findViewById(R.id.et_timeout);
        rg_mode = findViewById(R.id.rg_mode);
        TextView tvTitle = findViewById(R.id.tv_title);

        rg_mode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_advanced){
                    mode = ZIMCallInvitationMode.ADVANCED;
                }else if (checkedId == R.id.rb_general){
                    mode = ZIMCallInvitationMode.GENERAL;
                }
            }
        });

        callID = getIntent().getStringExtra("callId");
        if (callID == null){
            tvTitle.setText("创建会议");
        }else {
            tvTitle.setText("邀请他人");
            et_timeout.setEnabled(false);
        }

        findViewById(R.id.btn_create).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] member_user = et_member_users.getText().toString().split(";");
                if (member_user.length==0){
                    toast("请输入正确的userID");
                    return;
                }
                ArrayList<String> invitees = new ArrayList<>(Arrays.asList(member_user));
                if (callID == null){
                    ZIMCallInviteConfig config = new ZIMCallInviteConfig();
                    config.mode = mode;
                    config.timeout = Integer.parseInt(et_timeout.getText().toString());
                    CallManager.sharedInstance().callInvite(invitees, config, new ZIMCallInvitationSentCallback() {
                        @Override
                        public void onCallInvitationSent(String callID, ZIMCallInvitationSentInfo info, ZIMError errorInfo) {
                            if (errorInfo.code == ZIMErrorCode.SUCCESS){
                                InMeetingActivity.actionStart(MeetingInviteActivity.this,callID,MyApplication.sUserId);
                                toast("invite success");
                            }else {
                                toast("invite error,code:"+errorInfo.code+",message:"+errorInfo.message);
                            }
                        }
                    });
                }else {
                    ZIMCallingInviteConfig config = new ZIMCallingInviteConfig();
                    CallManager.sharedInstance().callingInvite(callID, invitees, config, new ZIMCallingInvitationSentCallback() {
                        @Override
                        public void onCallingInvitationSent(String callID, ZIMCallingInvitationSentInfo info, ZIMError errorInfo) {
                            if (errorInfo.code == ZIMErrorCode.SUCCESS){
                                toast("calling invite success");
                                finish();
                            }else {
                                toast("calling invite error,code:"+errorInfo.code+",message:"+errorInfo.message);
                            }
                        }
                    });
                }
            }
        });
    }
}